package com.ocom.srvpaccess.controller;

import com.ocom.common.constant.SrvpAccessDeviceUrl;
import com.ocom.common.entity.master.EntityDeviceLogin;
import com.ocom.common.entity.access.EntityMjDeviceInfo;
import com.ocom.common.entity.access.EntityMjYkm;
import com.ocom.common.enums.MachEnum;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.utils.RespUtilMach;
import com.ocom.srvpaccess.service.AreaInfoService;
import com.ocom.srvpaccess.service.DeviceLoginService;
import com.ocom.srvpaccess.service.MjDeviceInfoService;
import com.ocom.srvpaccess.service.MjYkmService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class MachParaController {


    @Autowired
    private MjDeviceInfoService mjDeviceInfoService;

    @Autowired
    private DeviceLoginService deviceLoginService;

    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private MjYkmService mjYkmService;

    private Logger logger = LoggerFactory.getLogger(MachParaController.class);


    // 获取设备参数
    @GetMapping(value = SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_getMachPara)
    public RespMachMessage getMachPara(Long companyId, String machNo){
        //判断参数
        if (companyId <=0 || machNo==null){
            return RespUtilMach.error(MachEnum.PARAMS_ERROR);
        }
        EntityMjDeviceInfo machPara = mjDeviceInfoService.getDeviceInfo(companyId, machNo);
        if (machPara == null){
            log.info("设备不存在,参数 【{}】【{}】",companyId,machNo);
            return RespUtilMach.error(MachEnum.PARAMS_ERROR);
        }
        //封装心跳间隔时间参数
        Map<String,Object> heartBeatMap=new HashMap<>();
        heartBeatMap.put("heart_data",30);
        //封装设备参数
        Map<String,Object> machineMap=new HashMap<>();


        machineMap.put("cosumerMode",machPara.getDeviceVerType());
        machineMap.put("faceAuthCode",machPara.getFaceAuthCode());
        machineMap.put("openType",machPara.getOpenType());
        machineMap.put("operationtype",machPara.getOpenType());
        List<EntityMjYkm> entityMjYkmList=mjYkmService.selectMjYkmList(companyId,machPara.getAreaId());
        if (entityMjYkmList.size()>0){
            List<Map<String,Object>> mjYkmlist=new ArrayList<>();
            for (EntityMjYkm entityMjYkm : entityMjYkmList) {
                if (entityMjYkm.getColorGreen()){
                    Map<String,Object> mjYkmMap=new HashMap<>();
                    mjYkmMap.put("jkmys","绿");
                    mjYkmMap.put("needletimes",0);
                    mjYkmMap.put("hsjc",entityMjYkm.getIsCheckRange()==false?0:entityMjYkm.getCheckRange()<=24?
                            1:entityMjYkm.getCheckRange()<=48?2:entityMjYkm.getCheckRange()<=72?3:entityMjYkm.getCheckRange()>72?4:0);
                    mjYkmlist.add(mjYkmMap);
                }
                if (entityMjYkm.getColorYellow()){
                    Map<String,Object> mjYkmMap=new HashMap<>();
                    mjYkmMap.put("jkmys","黄");
                    mjYkmMap.put("needletimes",0);
                    mjYkmMap.put("hsjc",entityMjYkm.getIsCheckRange()==false?0:entityMjYkm.getCheckRange()<=24?
                            1:entityMjYkm.getCheckRange()<=48?2:entityMjYkm.getCheckRange()<=72?3:entityMjYkm.getCheckRange()>72?4:0);
                    mjYkmlist.add(mjYkmMap);
                }

                if (entityMjYkm.getColorRed()){
                    Map<String,Object> mjYkmMap=new HashMap<>();
                    mjYkmMap.put("jkmys","红");
                    mjYkmMap.put("needletimes",0);
                    mjYkmMap.put("hsjc",entityMjYkm.getIsCheckRange()==false?0:entityMjYkm.getCheckRange()<=24?
                            1:entityMjYkm.getCheckRange()<=48?2:entityMjYkm.getCheckRange()<=72?3:entityMjYkm.getCheckRange()>72?4:0);
                    mjYkmlist.add(mjYkmMap);
                }
            }
            machineMap.put("openRules",mjYkmlist);
        }
        //封装IC卡参数
        Map<String,Object> cardMap=new HashMap<>();
        String password = null;
        String unitPassword = null;
        Integer informationSN = null;
        cardMap.put("password",password);
        cardMap.put("unitPassword",unitPassword);
        cardMap.put("informationSN",informationSN);


        //羊城通 参数yct_para说明
        Map<String,Object> yctMap=new HashMap<>();
        try {
            EntityDeviceLogin deviceLogin=deviceLoginService.selectYCT(companyId,machNo);
            if (deviceLogin!=null){

                yctMap.put("yctaccount",deviceLogin.getAccount());
                yctMap.put("yctpassword",deviceLogin.getPassword());
                yctMap.put("sender",deviceLogin.getSender());
                yctMap.put("sign_key",deviceLogin.getSignKey());
            }
        }catch (Exception e){
            return RespUtilMach.error(MachEnum.PARAMS_ERROR);
        }


        //封装返回参数
        Map<String, Object> map = new HashMap<>();
        //标识本次通讯
        map.put("jobId", machPara.getParaJobid());
        //心跳间隔时间参数
        map.put("heart_data",heartBeatMap);
        //设备参数
        map.put("machine_data",machineMap);
        //IC卡参数
        map.put("card_data",cardMap);
        //羊城通
        map.put("yct_para",yctMap);
        return RespUtilMach.success(map);
    }

    //更改设备参数状态（changeMachParaState）
    @PostMapping(value = SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_changeMachParaState)
    public RespMachMessage changeMachParaState(Long companyId, String machNo,String jobId){
        //判断参数
        if (companyId == null || machNo==null || jobId==null){
            return RespUtilMach.error(MachEnum.PARAMS_ERROR);
        }
        //修改门禁设备表mj_device_info中com_id、device_no匹配的记录
        try {
            mjDeviceInfoService.changeMachParaState(companyId, machNo,jobId);
        }catch (Exception exception){
            exception.printStackTrace();
        }

        Map<String, Object> map = new HashMap<>();
        //标识本次通讯
        map.put("updataFlag", 0);
        return RespUtilMach.success(map);
    }


    /*
     * @Description: 根据片区id获取中台设备数
     * @Author: lu 
     * @Date: 2022/4/12 14:07
     * @param comId: 
     * @param regionId: 
     * @param areaId:  
     * @return: com.ocom.common.resp.RespMachMessage
     **/
    @PostMapping(value = SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_getMachNumsByReginId)
    public RespMachMessage getMachNumsByReginId(Long comId, Integer regionId,Integer areaId){
        if (comId==null || regionId==null){
            return RespUtilMach.error(MachEnum.REQUIRED_NULL);
        }
        Map<String,Object> map=new HashMap<>();

        try {
            int count=mjDeviceInfoService.selectMachCount(comId,regionId,areaId);
            map.put("comId",comId);
            map.put("regionId",regionId);
            map.put("areaId",areaId);
            map.put("nums",count);
            return RespUtilMach.success(map);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("根据片区id获取中台设备数# 【异常】，【{}】",e);
            return RespUtilMach.error(MachEnum.ERR);
        }

    }


    /*
     * @Description:
     * @Author: lu 
     * @Date: 2022/4/12 18:05
     * @param companyId: 
     * @param machNo:  
     * @return: com.ocom.common.resp.RespMachMessage
     **/
    @PostMapping(value = SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_getReginIdByMach)
    public RespMachMessage getReginIdByMach(Long comId, String machNo){
        if (comId==null || machNo==null){
            return RespUtilMach.error(MachEnum.REQUIRED_NULL);
        }
        Map<String,Object> map=new HashMap<>();

        try {
            int areaId=mjDeviceInfoService.selectAreaId(comId,machNo);
            if (areaId==0){
                map.put("comId",comId);
                map.put("regionId",0);
            }else {
                int regionId=areaInfoService.selectRegionId(comId,areaId);
                map.put("comId",comId);
                map.put("regionId",regionId);
            }
            return RespUtilMach.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return RespUtilMach.error(MachEnum.ERR);
        }

    }



}
